package com.example.aop.aoputil;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Create by Z on 2021/2/20.
 */
//@Component
//@Aspect
public class LogAspect {

    public static Logger log = LoggerFactory.getLogger(LogAspect.class);

    @Pointcut("execution(public * com.example.aop.service.*.*(..))")
    public void pointcut1() {

    }

    @Pointcut("execution(public * com.example.aop.controller.*.*(..))")
    public void pointcut2() {

    }

    //@Before("pointcut1()")
    public void servicebeforeLog() {
        System.out.println("这是service请求开始");
    }

    //@After("pointcut1()")
    public void serviceafterLog() {
        System.out.println("这是service请求结束");
    }



    @After("pointcut2()")
    public void controllerafterLog() {
        System.out.println("这是controller请求结束");
    }

    //@Before("pointcut2()")
    public void a(JoinPoint joinPoint) {
        System.out.println("-------------------------获取请求信息---------------------------");
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        log.info("===============================Start========================");
        log.info("IP                 : {}", request.getRemoteAddr());
        log.info("URL                : {}", request.getRequestURL().toString());
        log.info("HTTP Method        : {}", request.getMethod());
        log.info("Class Method       : {}.{}", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName());
    }
    @Before("pointcut2()")
    public void b(JoinPoint joinPoint) {
        System.out.println("这是controller请求开始");
    }


    //@Around("pointcut2()")
    public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        long start = System.currentTimeMillis();
        Object result = proceedingJoinPoint.proceed();
        log.info("Request Params       : {}", getRequestParams(proceedingJoinPoint));
        log.info("Result               : {}", result);
        log.info("Time Cost            : {} ms", System.currentTimeMillis() - start);

        return result;
    }

    private Map<String, Object> getRequestParams(ProceedingJoinPoint proceedingJoinPoint) {
        Map<String, Object> requestParams = new HashMap<>();

        //参数名
        String[] paramNames = ((MethodSignature)proceedingJoinPoint.getSignature()).getParameterNames();
        //参数值
        Object[] paramValues = proceedingJoinPoint.getArgs();

        for (int i = 0; i < paramNames.length; i++) {
            Object value = paramValues[i];

            //如果是文件对象
            if (value instanceof MultipartFile) {
                MultipartFile file = (MultipartFile) value;
                value = file.getOriginalFilename();  //获取文件名
            }

            requestParams.put(paramNames[i], value);
        }

        return requestParams;
    }
}
